<template>
  <div class="base-rate">
    <div class="title">
      <span>
        基础运价
        <span v-if="formState.beginTime" style="color: red">( 生效时间：{{ dayjs(formState.beginTime).format("YYYY-MM-DD HH:mm:ss") }} --- 2099-12-31 23:59:59 )</span>
      </span>
      <div>
        <a-button @click="editHandle">编辑</a-button>
      </div>
    </div>
    <div class="table-content">
      <vxe-table :data="formState.itemList" border show-overflow ref="rateTable" height="200" :row-config="{ isHover: true }" :sort-config="{ trigger: 'cell' }">
        <vxe-column field="countryList" title="国家" min-width="120" fixed="left">
          <template #default="{ row }">
            {{ getCountryLabel(row.countryList) }}
          </template>
        </vxe-column>
        <vxe-column field="region" title="分区" show-overflow="title" min-width="140" fixed="left"></vxe-column>
        <vxe-column field="city" title="城市" show-overflow="title" min-width="140" fixed="left"></vxe-column>
        <vxe-colgroup v-for="(item, index) in formState.weightSegmentList || []" :key="item.title" :title="`${item.minValue}-${item.maxValue}`">
          <template v-if="['UNIT'].includes(item.type)">
            <vxe-column :field="item.title" title="单价" min-width="140">
              <template #default="{ row }">
                {{ row.detailsList[index].unitPrice }}
              </template>
            </vxe-column>
          </template>
          <template v-else-if="['AMOUNT'].includes(item.type)">
            <vxe-column :field="item.title" title="金额" min-width="140">
              <template #default="{ row }">
                {{ row.detailsList[index].amount }}
              </template>
            </vxe-column>
          </template>
          <template v-else-if="item.type === 'CON_WEIGHT'">
            <vxe-column field="firstWeight" title="首重重量" min-width="140">
              <template #default="{ row }">
                {{ row.detailsList[index].firstWeight }}
              </template>
            </vxe-column>
            <vxe-column field="firstPrice" title="首重价格" min-width="140">
              <template #default="{ row }">
                {{ row.detailsList[index].firstPrice }}
              </template>
            </vxe-column>
            <vxe-column field="continuationWeight" title="续重重量" min-width="140">
              <template #default="{ row }">
                {{ row.detailsList[index].continuationWeight }}
              </template>
            </vxe-column>
            <vxe-column field="continuationPrice" title="续重价格" min-width="140">
              <template #default="{ row }">
                {{ row.detailsList[index].continuationPrice }}
              </template>
            </vxe-column>
          </template>
        </vxe-colgroup>
      </vxe-table>
    </div>
  </div>
</template>

<script lang="ts" setup>
import dayjs from "dayjs";
import { Ref, inject, toRefs } from "vue";

const props = withDefaults(
  defineProps<{
    visible: boolean;
    formState: Partial<WarehouseEntryChannel.MakeOfferForm>;
  }>(),
  {}
);
const emit = defineEmits<{
  (e: "edit"): void;
}>();
const countryOpts = inject<Ref<Array<Base.GeneraDataType>>>("countryOpts");
const getCountryLabel = (ids: string[]) => {
  const result = [];
  for (let i = 0; i < ids.length; i++) {
    const id = ids[i];
    const { countryRegionName } = countryOpts?.value.find((t: Base.GeneraDataType) => t.id === id) || {};
    result.push(countryRegionName);
  }
  return result.join(",");
};
const { formState } = toRefs(props);
const editHandle = () => {
  emit("edit");
};
</script>
<style lang="stylus" scoped>
.base-rate{
    .title{
        display: flex;
        justify-content: space-between;
        align-items: center;
        font-size: 16px;
        font-weight: 600;
        padding-bottom: 12px;
    }
}
</style>
